<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
cfc('core')->checklogin();
$set=$this->plugin_setting('setting');
if(!$set['black']){
	$set['black']=5;
}
if(!$set['time']){
	$set['time']=24;
}
if($_RQ['op']=='main'){
	$member=cfc('core')->member_show($_SESSION['uid']);
	$store=pdo_get('jy_mapfuwu_store',array('mid'=>$_SESSION['uid']));
	if($store['id']){
		$this->model('store')->check_busy($store['id'],true,true);
	}
	$store['image']=$store['image']?:$member['avatar'];
	$store['image']=tomedia($store['image']);
	if($store['busy']==2){
		//exi('您还有未处理的订单，不可以接新订单！','error','jy_mapfuwu/manage.store_order/');
	}
	if($store['stop']==1){
		exi('您的号已被禁！！','error','jy_mapfuwu/manage.index/');
	}
	if($store['status']!=1){
		exi('您的号还未被审核通过！','error','jy_mapfuwu/manage.index/');
	}
	if($store['notimes']>=$set['black']){
		if(($store['update_time']+3600*$set['time'])>time()){
			exi('您拒单超过五次，冻结24小时，暂时不能接单','error');
		}else{
			pdo_update('jy_mapfuwu_store',array('stop'=>0,'notimes'=>0),array('mid'=>$_SESSION['uid']));
		}
	}
	//pdo_update('jy_mapfuwu_store',array('busy'=>1,'uplat_time'=>TIMESTAMP),array('mid'=>$_SESSION['uid']));
	//$store['busy']=1;
	if(!$store['image']){
		$store['image']=pdo_getcolumn('core_members',array('id'=>$_SESSION['uid']),'avatar');
	}
	$return['store']=$store;
	$return['info']=array('show'=>0);
	$return['nomarkers']=array(
			'title'=>'我',
			'iconPath'=>$return['pluginset']['map_icon']?tomedia($return['pluginset']['map_icon']):tomedia('../addons/jy_mapfuwu/resource/images/map_icon.png'),
			'width'=>36,
			'height'=>36,
			'customCallout'=>array(
				'anchorY'=>-50,
				'display'=>'ALWAYS',
			),
	);
	exi($return);
}
if($_RQ['op']=='ok'){
	$post=get_postdata();
	$data=array_fields(array('price','jjuli','price2','lat','lat2','order'),$post);	
	$order=pdo_get('jy_mapfuwu_order',array('id'=>intval($post['orderid'])?:intval($post['id'])));
	$storeid=$this->model('store')->getpai($order);
	if($_SESSION['os']=='wxapp'){
		exi('已接单');
	}else{
		exi('已接单','','jy_mapfuwu/manage.store_order//orderid='.$post['orderid']);
	}
}
if($_RQ['op']=='no'){
	$post=get_postdata();
	$orderid=intval($post['orderid'])?:intval($post['id']);
	$order=pdo_get('jy_mapfuwu_order',array('id'=>$orderid));
	$this->model('store')->nopai($order);
	exi('已拒绝','','/manage.index/');
}
if($_RQ['op']=='dw'){
	$post=get_postdata();
	pdo_update('jy_mapfuwu_store',array('latlng'=>ijson_encode($post['latlng']),'uplat_time'=>TIMESTAMP),array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	exi($return);
}
if($_RQ['op']=='getorder'){
	$lat_lng=array('lat'=>$_RQ['lat'],'lng'=>$_RQ['lng']);
	$isexi=$_RQ['isexi']?true:false;
	$store=pdo_get('jy_mapfuwu_store',array('mid'=>$_SESSION['uid']));
	$check_busy=$this->model('store')->check_busy($store['id'],true,$isexi);
	if(!$check_busy){
		exi('none');
	}
	//先查询待接收订单
	$store=pdo_get('jy_mapfuwu_store',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	$getorder=pdo_get('jy_mapfuwu_order',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store['id'],'status'=>6));
	if($getorder['id']){
		$getorder['lat']=json_decode($getorder['lat'],true);
		exi($getorder);
	}else{
		$orders=pdo_getall('jy_mapfuwu_order',array('uniacid'=>$_SESSION['uniacid'],'status'=>1));
		$fanwei=intval($set['fanwei'])?:30;
		foreach($orders as $k=>&$v){
			$v['latlng']=json_decode($v['lat'],true);
			$v['lat']=json_decode($v['lat'],true);
			$v['nowjuli']=cfc('tools')->getdistance($lat_lng['lng'],$lat_lng['lat'],$v['latlng']['lng'],$v['latlng']['lat']);
			$v['nowjuli']=intval($v['nowjuli']/1000);
			if($fanwei && $fanwei<$v['nowjuli']){
				unset($orders[$k]);
			}
		}
		if($orders[0]){
			paixu($orders,'nowjuli');
			$res=$this->model('store')->getorder($orders[0]);
		}
		if($res){
			exi($orders[0]);
		}else{
			exi('none');
		}
	}
}
?>